﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNet.Common.NHibernateUtil.QueryFilter;
using NHibernate;
using NHibernate.Transform;
using System.Collections;
using System.IO;
using ZLCMS.Business.Util;
using ZLCMS.Commands.Pub.Util;
using ZLCMS.Commands.User.Util;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Product
{
    public partial class ProductSalesSummary : System.Web.UI.Page
    {

        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);

            //设置分页大小
            int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;

            //设置页码
            int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;
            string sql = @"  from shop_product_sales_sum sm inner JOIN shop_order_product p on sm.PRODUCT_SKU_NO=p.SKU_PRODUCT_NO where 1=1 ";
            QueryFilterList filters = new QueryFilterList();
            if (!string.IsNullOrEmpty(Request["productNo"]))
            {
                filters.Add(new StringFilter("sm.PRODUCT_NO", "product", "%" + Request["productNo"] + "%", StringCompairType.Like));
            }

            if (!string.IsNullOrEmpty(Request["start"]))
            {
                start.Text = Request["start"];
                filters.Add(new NumberFilter("sm.DATE", "start", DateTime.Parse(Request["start"]), NumberCompairType.Ge));
            }
            if (!string.IsNullOrEmpty(Request["end"]))
            {
                end.Text = Request["end"];
                filters.Add(new NumberFilter("sm.DATE", "end", DateTime.Parse(Request["end"]), NumberCompairType.Le));
            }
            string departmentCode = "";
            if (AuthUtil.CheckAuthorization("PRODUCT_MAN", "VIEW_ALL", UserKit.GetUserAccount()))
            {
                //查看所有汇总
                departmentCode = "0000";
            }
            else if (AuthUtil.CheckAuthorization("PRODUCT_MAN", "VIEW_OWNER", UserKit.GetUserAccount()))
            {
                //查看本部门的汇总
                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    departmentCode = session.CreateSQLQuery("select dept_code from sys_user_info where user_account=:acc")
                        .SetParameter("acc", UserKit.GetUserAccount()).UniqueResult<string>();
                }
            }
            else
            {
                //没有权限访问
                departmentCode = "no_dept";
            }
            filters.Add(new StringFilter("sm.DEPT_CODE", "dept", departmentCode));

            foreach (DataFilter filter in filters)
            {
                sql += " and " + filter.GetSection();
            }

            using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                //判断是否导出EXCEL操作
                if (!string.IsNullOrEmpty(Request["exportexcel"]))
                {
                    IQuery query = session.CreateSQLQuery("select sm.*,p.PRODUCT_NAME,p.PRODUCT_IMAGE as THUMBNAIL_IMG,p.SKU_DATA as  PARAM_CODE" + sql + " order by sm.DATE desc ")
                        .SetResultTransformer(Transformers.AliasToEntityMap);

                    filters.SetUp(query);

                    IList<IDictionary> list = query.List<IDictionary>();


                    StringWriter sw = new StringWriter();
                    sw.WriteLine("产品编号\t产品SKU编号\t产品名称\t日期\t当日销售额\t当天销量");

                    foreach (IDictionary item in list)
                    {
                        sw.WriteLine("'" + item["PRODUCT_NO"] + "\t" + item["PRODUCT_SKU_NO"] + "\t" + item["PRODUCT_NAME"] + SkuProductKit.GetSkuItemNames(item["PARAM_CODE"] as string) + "\t"
                             + item["DATE"] + "\t" + item["SALES_AMOUNT"] + "\t" + item["SALES_COUNTER"] + "\t");
                    }
                    sw.Close();

                    OutputKit.OutputExcel(Response, sw);
                }
                else
                {

                    IQuery query = session.CreateSQLQuery("select count(*) " + sql );
                    filters.SetUp(query);

                    //设置记录总数
                    _framePage.RecordCount = Convert.ToInt32(query.UniqueResult());

                    query = session.CreateSQLQuery("select sm.*,p.PRODUCT_NAME,p.PRODUCT_IMAGE as THUMBNAIL_IMG,p.SKU_DATA as  PARAM_CODE " + sql + " order by sm.DATE desc ")
                        .SetFirstResult((pageIndex - 1) * pageSize)
                        .SetMaxResults(pageSize);
                    filters.SetUp(query);

                    //设置数据源
                    repeater1.DataSource = query.SetResultTransformer(Transformers.AliasToEntityMap).List();
                }
            }

            repeater1.DataBind();
        }
    }
}